package com.gtlab1207.br_security.ctrls;


import com.gtlab1207.br_security.common.Codes;
import com.gtlab1207.br_security.common.Result;
import com.gtlab1207.br_security.models.domain.AbacAttribute;
import com.gtlab1207.br_security.services.AbacAttributeService;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 业务控制层
 */
@RestController
@RequestMapping("/abac/attribute")
public class AbacAttributeCtrl extends BaseCtrl {
    @Autowired
    private AbacAttributeService abacAttributeService;

    /**
     * 查询全部
     * @param type
     * @return
     */
    @GetMapping("/info")
    public Result selectAbacAttribute(@RequestParam(value = "type",required = true) String type){
        try {
            Map<String, Object> params = new HashMap<>();
            if(type!=null){
                type=type.trim();
            }
            params.put("type",type);
            Map<String, Object> map = abacAttributeService.selectAbacAttribute(params);
            return this.send(map);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    /**
     * 添加信息
     * @param abacAttribute
     * @return
     */
    @PostMapping("/info")
    public Result addAbacAttribute(@Valid AbacAttribute abacAttribute){
        try {
            Integer result = abacAttributeService.insertAbacAttribute(abacAttribute);
            if(result==0){
                return this.send(1101,"新增失败，请重新录入信息");
            }
            return this.send(Codes.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    /**
     * 修改信息
     * @param abacAttribute
     * @return
     */
    @PutMapping("/info")
    public Result updateAbacAttribute(@Valid AbacAttribute abacAttribute){
        try {
            Integer result = abacAttributeService.updateAbacAttribute(abacAttribute);
            if(result==0){
                return this.send(1102,"修改失败，请重新输入信息");
            }
            return this.send(Codes.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/info/{id}")
    public Result deleteAbacAttribute(@PathVariable Integer id){
        try {
            Integer result = abacAttributeService.deleteAbacAttribute(id);
            if(result==0){
                return this.send(1100,"该信息不存在");
            }
            return this.send(Codes.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }
}
